<?php
namespace Swiftx\Bundle\WeChat\Model\Mall;
use Swiftx\Bundle\WeChat\Interfaces\Common\EntityModel;
use Swiftx\Bundle\WeChat\Model\Seller\Image as SellerImage;

/**
 * 商品模型类
 */
class Goods extends EntityModel {

    /**
     * 所属企业
     * @var int
     * @orm constraint no-null
     * @orm type char(36)
     * @orm column Company
     */
    protected $company;

    /**
     * 商品标题
     * @var string
     * @orm constraint no-null
     * @orm type varchar(100)
     * @orm column Title
     */
    protected $title;

    /**
     * 商品描述
     * @var int
     * @orm constraint no-null
     * @orm type text
     * @orm column Description
     */
    protected $description;

    /**
     * 商品货号
     * @var string
     * @orm constraint no-null
     * @orm type varchar(100)
     * @orm column Number
     */
    protected $number;

    /**
     * 是否提供发票
     * @var int
     * @orm constraint no-null
     * @orm type tinyint(1)
     * @orm column Invoice
     */
    protected $invoice;

    /**
     * 是否提供保修
     * @var int
     * @orm constraint no-null
     * @orm type tinyint(1)
     * @orm column Guarantee
     */
    protected $guarantee;

    /**
     * 是否提供退货
     * @var int
     * @orm constraint no-null
     * @orm type tinyint(1)
     * @orm column IsReturn
     */
    protected $isReturn;

    /**
     * 是否支持物流
     * @var int
     * @orm constraint no-null
     * @orm type tinyint(1)
     * @orm column LogisticsDistribution
     */
    protected $logisticsDistribution;

    /**
     * 是否支持自提
     * @var int
     * @orm constraint no-null
     * @orm type tinyint(1)
     * @orm column LogisticsSelf
     */
    protected $logisticsSelf;

    /**
     * 上架时间
     * @var int
     * @orm constraint no-null
     * @orm type int(10)
     * @orm column SellTime
     */
    protected $sellTime;

    /**
     * 创建时间
     * @var int
     * @orm constraint no-null
     * @orm type int(10)
     * @orm column CreateTime
     */
    protected $createTime;

    /**
     * 默认属性生成
     */
    public function __construct(){
        parent::__construct();
        $this->createTime = time();
    }

    /**
     * 获取时间戳
     * @return int
     */
    public function getCreateTimeStamp():int{
        return $this->createTime;
    }

    /**
     * 获取格式化的时间
     * @param string $format
     * @return string
     */
    public function getCreateTimeFormat(string $format='Y-m-d H:i:s'):string {
        return date($format, $this->getCreateTimeStamp());
    }

    /**
     * 设置标题
     * @param string $value
     */
    public function setTitle(string $value){
        $this->title = $value;
    }

    /**
     * 获取标题
     * @return string
     */
    public function getTitle():string {
        return $this->title;
    }

    /**
     * 设置所属企业
     * @param string $company
     */
    public function setCompany(string $company){
        $this->company = $company;
    }

    /**
     * 读取企业ID
     * @return string
     */
    public function getCompanyID():string {
        return $this->company;
    }

    /**
     * 读取介绍
     * @return string
     */
    public function getDescription():string {
        return $this->description;
    }

    /**
     * 设置商品描述
     * @param string $value
     */
    public function setDescription(string $value){
        $this->description = $value;
    }

    /**
     * 读取商品货号
     * @return string
     */
    public function getNumber():string {
        return $this->number;
    }

    /**
     * 设置商品货号
     * @param string $value
     */
    public function setNumber(string $value){
        $this->number = $value;
    }

    /**
     * 读取是否提供发票
     * @return bool
     */
    public function getInvoice():bool {
        return $this->invoice==1;
    }

    /**
     * 设置是否提供发票
     * @param bool $value
     */
    public function setInvoice(bool $value){
        $this->invoice = $value?1:0;
    }

    /**
     * 是否提供保修服务
     * @return bool
     */
    public function getGuarantee():bool {
        return $this->guarantee==1;
    }

    /**
     * 设置是否提供保修服务
     * @param bool $value
     */
    public function setGuarantee(bool $value){
        $this->guarantee = $value?1:0;
    }

    /**
     * 是否支持退货
     * @return bool
     */
    public function getIsReturn():bool {
        return $this->isReturn==1;
    }

    /**
     * 设置是否支持退货
     * @param bool $value
     */
    public function setIsReturn(bool $value){
        $this->isReturn = $value?1:0;
    }

    /**
     * 是否支持物流
     * @return bool
     */
    public function getLogisticsDistribution():bool {
        return $this->logisticsDistribution==1;
    }

    /**
     * 设置物流配送支持
     * @param bool $value
     */
    public function setLogisticsDistribution(bool $value){
        $this->logisticsDistribution = $value?1:0;
    }

    /**
     * 是否支持自提
     * @return bool
     */
    public function getLogisticsSelf():bool {
        return $this->logisticsSelf == 1;
    }

    /**
     * 设置是否支持自提
     * @param bool $value
     */
    public function setLogisticsSelf(bool $value){
        $this->logisticsSelf = $value?1:0;
    }

    /**
     * 获取上架时间戳
     * @return int
     */
    public function getSellTimeStamp():int{
        return $this->sellTime;
    }

    /**
     * 设置上架时间戳
     * @param int $value
     */
    public function setSellTimeStamp(int $value){
        $this->sellTime = $value;
    }

    /**
     * 获取所有商品相册
     * @return SellerImage
     */
    public function getImage():SellerImage {
        $query = $this->entityManager->builder(Image::class);
        $query->conditionEqualString('goods', $this->getID());
        /** @var Image $relation */
        $relation = $query->fetch();
        return $relation->getImage();
    }

    /**
     * 获取所有商品相册
     * @return SellerImage[]
     */
    public function getImages():array {
        $query = $this->entityManager->builder(Image::class);
        $query->conditionEqualString('goods', $this->getID());
        $result = [];
        /** @var Image $item */
        foreach ($query->list() as $item)
            $result[] = $item->getImage();
        return $result;
    }

    /**
     * 获取全部选项
     * @return Spec[]
     */
    public function getSpecs():array {
        $query = $this->entityManager->builder(Spec::class);
        $query->conditionEqualString('goods', $this->getID());
        return $query->list();
    }

    /**
     * 获取全部选项
     * @return Attribute[]
     */
    public function getAttributes():array {
        $query = $this->entityManager->builder(Attribute::class);
        $query->conditionEqualString('goods', $this->getID());
        return $query->list();
    }

    /**
     * 库存总数
     * @return int
     */
    public function getStock():int{
        $specs = $this->getSpecs();
        $count = 0;
        foreach ($specs as $spec)
            $count+=$spec->getStock();
        return $count;
    }

    /**
     * 最高售价
     * @return float
     */
    public function getMaxPrice():float {
        $query = $this->entityManager->builder(Spec::class);
        $query->conditionEqualString('goods', $this->getID());
        /** @var Spec $spec */
        $spec = $query->orderBy('price', false)->fetch();
        return $spec->getPrice();
    }

    /**
     * 最低售价
     * @return float
     */
    public function getMinPrice():float {
        $query = $this->entityManager->builder(Spec::class);
        $query->conditionEqualString('goods', $this->getID());
        /** @var Spec $spec */
        $spec = $query->orderBy('price', true)->fetch();
        return $spec->getPrice();
    }

}